package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.Family;
import com.tgu.home_action.pojo.OldMan;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.annotation.ApiJsonObject;
import com.tgu.home_action.pojo.annotation.ApiJsonProperty;
import com.tgu.home_action.pojo.annotation.MyLog;
import com.tgu.home_action.service.OldManService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api(tags = "老人信息管理")
@Validated
@RestController
@RequestMapping("/family/oldMan")
public class OldManController {
    @Autowired
    private Logger logger;
    @Autowired
    private OldManService oldManService;

    @MyLog(actionId = 8, type = 2, role = "family")
    @ApiOperation("插入老人信息")
    @PreAuthorize("hasRole('family')")
    @PostMapping
    public ResponseData<OldMan> insertOldMan(@Valid @RequestBody OldMan oldMan, @ApiIgnore @ModelAttribute("familyUser") Family familyUser) {
        return oldManService.insertOldMan(oldMan, familyUser);
    }

    @ApiOperation("查询老人信息")
//    @PreAuthorize("hasRole('family')")
    @GetMapping
    public ResponseData<List<OldMan>> getOldMen(@ApiIgnore @ModelAttribute("familyUser") Family familyUser) {
        return oldManService.getOldMen(familyUser);
    }

    @MyLog(actionId = 8, type = 3, role = "family")
    @ApiOperation("修改老人信息")
    @PreAuthorize("hasRole('family')")
    @PutMapping
    public ResponseData<Object> updateOldMan(@Validated(OldMan.update.class) @RequestBody OldMan oldMan, @ApiIgnore @ModelAttribute("familyUser") Family familyUser) {
        return oldManService.updateOldMan(oldMan, familyUser);
    }

    @MyLog(actionId = 8, type = 4, role = "family")
    @ApiOperation("删除老人信息")
    @PreAuthorize("hasRole('family')")
    @DeleteMapping
    public ResponseData<Object> deleteOldMan(@ApiJsonObject(name = "DeleteOldManId", value =
    @ApiJsonProperty(key = "id", description = "老人信息ID")) @RequestBody Map<String, Object> param, @ApiIgnore @ModelAttribute("familyUser") Family familyUser) {
        int id = (int) param.get("id");
        return oldManService.deleteOldMan(id, familyUser);
    }

}
